package com.example.ourradio;

import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {
	public final static String EXTRA_MESSAGE = "com.example.ourradio.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	// Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Closing Activity")
            .setMessage("Are you sure you want to close this activity?")
            .setPositiveButton("Logout", new DialogInterface.OnClickListener() {
            	@Override
            	public void onClick(DialogInterface dialog, int which) {
            		finish();
            		// Save preferences
    		        SharedPreferences settings = getSharedPreferences(
    		            Utils.PREFS_NAME, 0
    		        );
    		        String accessToken = 
    		        	settings.getString(Utils.OAUTH_TOKEN, null);
    			    SharedPreferences.Editor editor = settings.edit();
    			    editor.putString(Utils.OAUTH_TOKEN, null);
    			    editor.commit();
    			    // Logout
    			    Utils.logout(accessToken);
    			}
            })
            .setNeutralButton(
            	"Stay logged in", 
            	new DialogInterface.OnClickListener() {
            		@Override
            		public void onClick(DialogInterface dialog, int which) {
            			finish();    
            		}
            	}
            )
            .setNegativeButton("Cancel", null)
            .show();
    }
    
    public void openFeed(View view) {
    	Intent intent = new Intent(this, FeedActivity.class);
        startActivity(intent);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                openSearch();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    private void openSearch() {
    }

    private void openSettings() {
    }
}
